package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.admin.AdminConstants;
import com.heima.common.exception.CustomException;
import com.heima.feigns.article.ArticleFeign;
import com.heima.feigns.wemedia.WemediaFeign;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Date;

@Service
public class ApUserRealnameServiceimpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {
        //参数校验
        if (dto == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();

        Page<ApUserRealname> pageRes = new Page<>(dto.getPage(), dto.getSize()); //封装分页条件

        //封装查询条件
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();

        if (dto.getStatus() != null) {
            queryWrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }


        IPage<ApUserRealname> resultPage = this.page(pageRes, queryWrapper);  //执行分页查询
        //封装返回结果
        PageResponseResult pageResponseResult = new PageResponseResult((int) resultPage.getCurrent(), (int) resultPage.getSize(), (int) resultPage.getTotal());
        System.out.println(resultPage.getRecords());
        pageResponseResult.setData(resultPage.getRecords());
        return pageResponseResult;
    }

    @Resource
    ApUserMapper apUserMapper;

    @Override
   @GlobalTransactional(rollbackFor = Exception.class)
   // @Transactional
    public ResponseResult authPass(AuthDto dto) {
        //参数校验
        if (dto == null || dto.getId() == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //获取实名认证信息
        ApUserRealname userRealname = this.getById(dto.getId());
        //校验
        if (userRealname == null || userRealname.getUserId() == null) {
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //校验数据状态是否为未审核状态
        if (userRealname.getStatus() != 1) {
            throw new CustomException(AppHttpCodeEnum.DATA_STATUS_ERROR);
        }

        //根据用户id查询
        ApUser apUser = apUserMapper.findById(userRealname.getUserId());
        //判断用户数据是否为空
        if (apUser == null) {
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //修改数据状态为已审核状态
        userRealname.setStatus(AdminConstants.PASS_AUTH);
        this.updateById(userRealname);

        //创建对应自媒体用户
        WmUser wmUser = createWmUser(apUser);

        //创建作者
        createAuthor(wmUser);

        return ResponseResult.okResult();
    }

    @Resource
    WemediaFeign wemediaFeign;

    private WmUser createWmUser(ApUser apUser) {
        WmUser wmUser = wemediaFeign.findByName(apUser.getName());
        //判断是否已有对应的自媒体用户存在
        if (wmUser != null) {
            throw new CustomException(AppHttpCodeEnum.DATA_EXIST);
        }
        //封装添加数据
        wmUser = new WmUser();

        wmUser.setApUserId(apUser.getId());

        wmUser.setName(apUser.getName());

        wmUser.setPassword(apUser.getPassword());

        wmUser.setSalt(apUser.getSalt());

        wmUser.setImage(apUser.getImage());

        wmUser.setPhone(apUser.getPhone());

        wmUser.setStatus(AdminConstants.AD_USER_ISSTATUS);

        wmUser.setType(0);

        wmUser.setCreatedTime(new Date());

        return wemediaFeign.save(wmUser);

    }


    @Resource
    ArticleFeign articleFeign;

    private void createAuthor(WmUser wmUser) {
        ApAuthor author = articleFeign.findByUserId(wmUser.getApUserId());
        if(author!=null){
            throw new CustomException(AppHttpCodeEnum.DATA_EXIST,"作者信息已经存在");
        }
        ApAuthor  newAuthor = new ApAuthor();
        newAuthor.setName(wmUser.getName());
        newAuthor.setType(2);
        newAuthor.setUserId(wmUser.getApUserId());
        newAuthor.setCreatedTime(new Date());
        newAuthor.setWmUserId(wmUser.getId());

       articleFeign.save(newAuthor);
    }


    @Override
    public ResponseResult authFail(AuthDto dto) {
        //参数校验
        if (dto == null || dto.getId() == null || StringUtils.isBlank(dto.getMsg())) {
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //获取实名认证信息
        ApUserRealname userRealname = this.getById(dto.getId());
        //校验
        if (userRealname == null || userRealname.getUserId() == null) {
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //校验数据状态是否为未审核状态
        if (userRealname.getStatus() != 1) {
            throw new CustomException(AppHttpCodeEnum.DATA_STATUS_ERROR);
        }

        //根据用户id查询
        ApUser apUser = apUserMapper.findById(userRealname.getUserId());
        //判断用户数据是否为空
        if (apUser == null) {
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //设置状态为审核失败
        userRealname.setStatus(AdminConstants.FAIL_AUTH);

        userRealname.setReason(dto.getMsg());

        this.updateById(userRealname);

        return ResponseResult.okResult();
    }
}
